﻿using System;
using System.Reflection;
using System.Runtime.Serialization;

namespace ElectronicCommerce.Framework.Payment
{  /// <summary>
    /// 
    /// </summary>
    public class PaymentException : Exception
    {
        public PaymentExceptionType ExceptionType { get; private set; }

        public PaymentException(PaymentExceptionType exceptionType)
            : base()
        {
            ExceptionType = exceptionType;
        }

        public PaymentException(PaymentExceptionType exceptionType, string message)
            : base(message)
        {
            ExceptionType = exceptionType;
        }

        public PaymentException(PaymentExceptionType excptionType, string message, Exception inner)
            : base(message, inner)
        {
            ExceptionType = excptionType;
        }

        protected PaymentException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            Type enumType = Assembly.Load(info.GetString("ExceptionTypeAssemblyKey")).GetType(info.GetString("ExceptionTypeTypeKey"));
            ExceptionType = (PaymentExceptionType)Enum.Parse(enumType, info.GetString("ExceptionTypeValueKey"));
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("ExceptionTypeAssemblyKey", ExceptionType.GetType().Assembly.FullName);
            info.AddValue("ExceptionTypeTypeKey", ExceptionType.GetType().ToString());
            info.AddValue("ExceptionTypeValueKey", Convert.ToInt32(ExceptionType));
        }

        public override string Message
        {
            get
            {
                string message = base.Message;
                if (string.IsNullOrEmpty(message))
                {
                    switch (ExceptionType)
                    {
                        case PaymentExceptionType.NoTestGatewayAddress:
                            message = Resources.NoTestGatewayAddress;
                            break;
                    }
                }

                return message;
            }
        }
    }
}
